﻿using ImageProcessing.Comm.Expression;
using ImageProcessing.Comm.LogHelp;
using ImageProcessing.HttpRequest;
using ImageProcessing.Model.DataCenter;

using Newtonsoft.Json;

namespace ImageProcessing.DataCenters
{
    public class DataCenter : IDataCenter
    {
        private readonly IHttpRequests _httpRequest;
        private readonly ILoggerHelper<DataCenter> _loggerHelper;
        private IConfiguration _configuration;

        public DataCenter(IConfiguration configuration, IHttpRequests httpRequest, ILoggerHelper<DataCenter> loggerHelper)
        {
            _configuration = configuration;
            _httpRequest = httpRequest;
            _loggerHelper = loggerHelper;
        }

        #region 文件上传

        /// <summary>
        /// 上传文件接口
        /// </summary>
        /// <param name="dCUploadCaseParMsg"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<DCUploadRetParMsg> UploadAsync(MemoryStream memoryStream)
        {
            var log = "";
            try
            {
                //var dataCenterUrl = _baseApollo.GetApolloValue("datacenter_endpoint");
                var dataCenterUrl = _configuration["service:dcmanager:url-test"];
                if (string.IsNullOrWhiteSpace(dataCenterUrl))
                {
                    log += $"获取到数据中心的地址是空";
                    _loggerHelper.Warn($"数据地址获取异常:{log}");
                    return null;
                }
                dataCenterUrl += "/dc/v1/file/uploadWithBucketName";
                log += $"拼接完成上传案件的url{dataCenterUrl}";
                var dataContent = new MultipartFormDataContent();

                //c#文件流读文件
                //using (FileStream fsRead = new FileStream(caseFilePath, FileMode.Open))
                //{
                //必须加最后一个字段 文件名称
                dataContent.Add(new StreamContent(memoryStream), "upfile", "file.jpg");
                return await _httpRequest.PostDataCenterAsync<DCUploadRetParMsg>(dataCenterUrl, dataContent);
                //}
                //var fileInfo = new FileInfo(caseFilePath);
                //fileInfo.Delete();
            }
            catch (Exception ex)
            {
                _loggerHelper.Error($"上传文件接口异常:{ex.Message}，log为 {log}", ex);
                throw;
            }
        }

        #endregion 文件上传

        #region 文件下载

        public async Task<Stream> Download(string fileId)
        {
            var log = "";
            //var dataCenterUrl = _baseApollo.GetApolloValue("datacenter_endpoint");
            var dataCenterUrl = _configuration["service:dcmanager:url-test"];
            if (string.IsNullOrWhiteSpace(dataCenterUrl))
            {
                log += $"获取到数据中心的地址是空";
                return null;
            }
            dataCenterUrl += $"/dc/v1/file/download?fileId={fileId}";
            log += $"拼接完成上传案件的url{dataCenterUrl}";
            try
            {
                return await _httpRequest.GetDataCenterFileAsync(dataCenterUrl);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        #endregion 文件下载

        //Task<DCUploadRetParMsg> IDataCenter.UploadAsync(DCUploadParMsg dCDownloadParMsg)
        //{
        //    throw new NotImplementedException();
        //}
        //public Task<string> DownLoad(DCDownloadParMsg dCDownloadParMsg)
        //{
        //    var log = "";
        //    var dataCenter = _baseApollo.GetApolloValue("datacenter_endpoint");
        //    if (string.IsNullOrWhiteSpace(dataCenter))
        //    {
        //        log+=$"获取到数据中心的地址是空";
        //        return null;
        //    }

        //    _httpRequest.GetDataCenterAsync<string>("https://cn.bing.com/");
        //    throw new NotImplementedException();
        //}
    }
}